home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / files / amiga / csrc720j.lzh / mbbackup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  5.8 KB  |  310 lines

  1.  
  2.    /* Backup
  3.    Used to back up message directory on other directory or drive
  4.    Deletes what's not on the source drive and copies what's not
  5.    on the backup drive.  N3EPY 1 May 1990
  6.    dir_list was taken from CBBS mbrestm.c
  7.    */
  8.  
  9. #include "sys\types.h"
  10. #include "sys\stat.h"
  11. #include "fcntl.h"
  12. #include "dos.h"
  13. #include "ctype.h"
  14. #include "time.h"
  15.  
  16. #define BUFSIZE 4096
  17. #define PATHSIZE 60
  18. #define true 1
  19. #define false 0
  20.  
  21. char buff[BUFSIZE];
  22. char spp[44], tpp[44], sname[800][13], tname[800][13];
  23. char srcpath[PATHSIZE], tgtpath[PATHSIZE];
  24. char *tempfile[2];
  25. char pathfile[PATHSIZE];
  26. int scount, tcount;
  27. int cmp();
  28. int xx;
  29. int prtok;
  30.  
  31. main(argc,argv)
  32.  
  33. int argc;
  34. char *argv[];
  35.  
  36. {
  37.   struct tm *timeptr;
  38.   time_t elapsed_seconds;
  39.  
  40.   printf("\n    ************  CBBS FILE BACKUP  ************\n");
  41.   printf("      for CBBS by Richard Johnson, N3EPY/AAR3PY\n");
  42.   printf("                    Version 1.5\n\n");
  43.   strcpy(srcpath,argv[1]);
  44.   strcpy(tgtpath,argv[2]);
  45.  
  46.   if(argc < 3)
  47.   {
  48.   printf("    Command format should be: MBBACKUP c:\\mb\\msgs\\ d:\\mb\\msgs\\ 1\n");
  49.   printf("    or: MBBACKUP [source path]\\ [target path]\\ [verbose switch]\n");
  50.   printf("    The verbose switch is optional. 1 will display all file actions.\n");
  51.   printf("    This will slow the process if there is a lot to do.  If you want\n");
  52.   printf("    to see just the essentials, omit the third argument or make it a 0.\n");
  53.   exit(0);
  54.   }
  55.   prtok = 0;
  56.  
  57.   if(argc == 4)
  58.   prtok = atoi(argv[3]);
  59.  
  60.   dir_list();
  61.   printf("Performing Deletion Operation\n");
  62.   tardel();
  63.   printf("Performing Copying Operation\n");
  64.   srccpy();
  65.   time(&elapsed_seconds);
  66.   timeptr = localtime(&elapsed_seconds);
  67.   printf("*** DONE at %2d:%2d:%2d on %2d/%2d/%2d ***\n", timeptr->tm_hour,
  68.        timeptr->tm_min, timeptr->tm_sec, timeptr->tm_mon + 1, timeptr->tm_mday,
  69.        timeptr->tm_year);
  70. }
  71. /*
  72.  *  Read the file names from the current directory and store
  73.  *  the results in an array but exclude any name that is not
  74.  *  made up of all numbers.
  75.  *  Sort the resulting list in ascending order.
  76.  */
  77.  
  78. dir_list()
  79. {
  80.   int x, y;
  81.   int c;
  82.   char *sfile_name[1];
  83.   char *tfile_name[1];
  84.   char *directory = "*.";
  85.   char spath[60];
  86.   char tpath[60];
  87.   xx = 1;
  88.   scount = 0;
  89.  
  90.   strcpy(spath,srcpath);
  91.   strcpy(tpath,tgtpath);
  92.   strcat(spath,directory);
  93.   strcat(tpath,directory);
  94.   sfile_name[0] = spath;
  95.   tfile_name[0] = tpath;
  96.  
  97.   printf("Reading source directory.....\n");
  98.   bdos(0x1a, (unsigned)spp, 0);
  99.   if(18==bdos(0x4e, (unsigned)sfile_name[0], 0))
  100.     {
  101.       printf("No message files are found\n");
  102.       exit(0);
  103.     }
  104.  
  105.   if (num( &spp[30])) strcpy(sname[scount++], &spp[30]);
  106.  
  107.   for(;;)
  108.   {
  109.     if(18==bdos(0x4f, 0, 0)) break;
  110.     if (num( &spp[30])) strcpy (sname[scount++], &spp[30]);
  111.   }
  112.   printf("There are %d message files\n", scount);
  113.   if (scount)
  114.     {
  115.       printf("***Sorting***\n");
  116.       qsort (sname, scount, 13, cmp);
  117.       for(x=0, y=1; x< scount; x++, y++)
  118.       {
  119.     if(prtok)
  120.     {
  121.     printf("%6s", sname[x]);
  122.     if(y == 10) { printf("\n"); y=0; }
  123.     }
  124.       }
  125.       if(prtok)
  126.       printf("\n");
  127.     }
  128.   printf("Reading target directory.....\n");
  129.   tcount = 0;
  130.   bdos(0x1a, (unsigned)tpp, 0);
  131.   if(18==bdos(0x4e, (unsigned)tfile_name[0], 0))
  132.     {
  133.       printf("No message files are found\n");
  134.       return;
  135.     }
  136.  
  137.   if (num( &tpp[30])) strcpy (tname[tcount++], &tpp[30]);
  138.  
  139.   for(;;)
  140.   {
  141.     if(18==bdos(0x4f, 0, 0)) break;
  142.     if (num( &tpp[30])) strcpy (tname[tcount++], &tpp[30]);
  143.   }
  144.   printf("There are %d message files\n", tcount);
  145.   if (tcount)
  146.     {
  147.       printf("***Sorting***\n");
  148.       qsort (tname, tcount, 13, cmp);
  149.       for(x=0, y=1; x< tcount; x++, y++)
  150.       {
  151.     if(prtok)
  152.     {
  153.     printf("%6s", tname[x]);
  154.     if(y == 10) { printf("\n"); y=0; }
  155.     }
  156.       }
  157.       if(prtok)
  158.       printf("\n");
  159.     }
  160. }
  161.  
  162. /*
  163.  *  Compare two strings converted to integers.
  164.  */
  165.  
  166. int cmp(n1,n2)
  167. char *n1;
  168. char *n2;
  169. {
  170.    return (atoi(n1) - atoi(n2));
  171.  
  172. }
  173.  
  174. /*
  175.  *  is the string a number?
  176.  */
  177.  
  178. num(p)
  179. char *p;
  180. {
  181.   for (; *p; p++) if (!isdigit(*p)) return false;
  182.   return true;
  183. }
  184.  
  185.  
  186. /*
  187.  *  Search the target directory for the filemanes in the
  188.  *  source directory.  If no match, copy that file to the
  189.  *  target directory
  190.  */
  191.  
  192. srccpy()
  193. {
  194.     int h = 0;
  195.     int i = 0;
  196.     int cpyok;
  197.     int lim = 0;
  198.  
  199.     for(h=0; h<scount; h++)
  200.     {
  201.     cpyok = 1;
  202.     for(i=lim; i<tcount; i++)
  203.     {
  204.         if(cpyok)
  205.         {
  206.         if(prtok)
  207.         printf("Comparing target %s and source %s for copy-ability!\n",tname[i],sname[h]);
  208.  
  209.         if(atoi(tname[i]) == atoi(sname[h]))
  210.             {
  211.             cpyok = 0;
  212.             lim = i+1;
  213.             }
  214.         }
  215.     }
  216.     if(cpyok)
  217.         {
  218.         tempfile[1] = sname[h];
  219.         cpyfile();
  220.         }
  221.     }
  222. }
  223.  
  224. /*   Check the source directory for files on the target
  225.  *   directory.  If no match, delete that file from the
  226.  *   directory.
  227.  */
  228.  
  229.  
  230. tardel()
  231. {
  232.     int h = 0;
  233.     int i = 0;
  234.     int delok;
  235.     int lim = 0;
  236.  
  237.     for(h=0; h<tcount; h++)
  238.     {
  239.     delok = 1;
  240.     for(i=lim; i<scount; i++)
  241.     {
  242.         if(delok)
  243.         {
  244.         if(prtok)
  245.         printf("Comparing target %s and source %s for deletion validation\n",tname[h],sname[i]);
  246.  
  247.         if(atoi(tname[h]) == atoi(sname[i]))
  248.             {
  249.             delok = 0;
  250.             lim = i+1;
  251.             }
  252.         }
  253.     }
  254.     if(delok)
  255.         {
  256.         tempfile[2] = tname[h];
  257.         delfile();
  258.         }
  259.     }
  260. }
  261.  
  262.  
  263.  
  264. cpyfile()
  265. {
  266.     int infile, outfile, bytes;
  267.     char spathfile[PATHSIZE];
  268.     char tpathfile[PATHSIZE];
  269.  
  270.  
  271.     strcpy(spathfile,srcpath);
  272.     strcpy(tpathfile,tgtpath);
  273.  
  274.     strcat(spathfile,tempfile[1]);
  275.     strcat(tpathfile,tempfile[1]);
  276.  
  277.     if((infile = open(spathfile, O_RDONLY | O_BINARY)) < 0)
  278.         {
  279.         printf("Can't open %s.\n",spathfile);
  280.         exit(1);
  281.         }
  282.  
  283.     if((outfile = open(tpathfile,
  284.         O_CREAT | O_WRONLY | O_BINARY, S_IWRITE)) < 0)
  285.         {
  286.         printf("Can't open %s.",tpathfile);
  287.         exit(1);
  288.         }
  289.     if(prtok)
  290.     printf("Copying %s.....\n",tpathfile);
  291.     while((bytes = read(infile, buff, BUFSIZE)) > 0)
  292.         write(outfile,buff,bytes);
  293.  
  294.     close(infile);
  295.     close(outfile);
  296.  
  297. }
  298.  
  299.  
  300. delfile()
  301. {
  302.     strcpy(pathfile,tgtpath);
  303.     strcat(pathfile,tempfile[2]);
  304.     if(prtok)
  305.     printf("Deleting %s.....\n",pathfile);
  306.     unlink(pathfile);
  307.  
  308. }
  309.  
  310.